package pers.feng1240.blog.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import pers.feng1240.blog.service.BlogService;
import pers.feng1240.blog.service.ForegroundService;
import pers.feng1240.blog.util.PageHelper;

/*
 * 前台页面
 */
@Controller
public class ForegroundController {

    @Resource
    private ForegroundService foregroundService; //service
    @Resource
    private BlogService blogService; //service
    
	/*
	 * 首页初始化
	 * @param pageIndex 页码
	 */
	@RequestMapping("index")
	public String getIndex(String pageIndex,HttpServletRequest request){
	    request.setAttribute("map", foregroundService.getPageList(PageHelper.pageIndex(pageIndex))); //获取博客
	    request.setAttribute("timesTop", foregroundService.timesTop());  //浏览排行
	    request.setAttribute("tags", foregroundService.tags());  //标签
	    request.setAttribute("theme", foregroundService.theme()); //主题列表
        request.setAttribute("action","blogIndex.do" ); //设置分页动态action
		return "forward:index.jsp";
	}
	
	/*
	 * 博客详情
	 * @param id 博客id
	 */
	@RequestMapping("getBlogById")
    public String getBlogById(int id,int times,HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("times", times+1);
		map.put("id", id);
		
		foregroundService.addTimes(map);		//访问量加一
		request.setAttribute("timesTop", foregroundService.timesTop());  //浏览排行
		request.setAttribute("tags", foregroundService.tags());  //标签
        request.setAttribute("blog", blogService.getById(id));  //获取详情
        return "forward:page/blog_item.jsp";
    }
	

	/*
	 * 博客列表
	 * @param pageIndex 页码
	 */
	@RequestMapping("blogIndex")
    public String getBlogIndex(String pageIndex,HttpServletRequest request){
        request.setAttribute("map", foregroundService.getPageList(PageHelper.pageIndex(pageIndex)));    //获取博客
        request.setAttribute("timesTop", foregroundService.timesTop());  //浏览排行
        request.setAttribute("tags", foregroundService.tags());  //标签
        request.setAttribute("action","blogIndex.do" ); //设置分页动态action
        return "forward:page/blog.jsp";
    }

	/*
	 * 根据标签查看博客列表
	 * @param pageIndex 页码
	 * @param id 标签id
	 */
	@RequestMapping("getBlogsByTag")
	public String getBlogsByTag(String pageIndex,int id,HttpServletRequest request){
	    request.setAttribute("map", foregroundService.getPageListByTag(PageHelper.pageIndex(pageIndex),id));    //获取博客
	    request.setAttribute("timesTop", foregroundService.timesTop());  //浏览排行
        request.setAttribute("tags", foregroundService.tags());  //标签
        request.setAttribute("action","getBlogsByTag.do" ); //设置分页动态action
	    return "forward:page/blogByTag.jsp";
	}
	
	/*
	 * 根据博客主题获取博客列表
	 * @param pageIndex 页码
	 * @param id 主题id
	 */
	@RequestMapping("getBlogsByTheme")
	public String getBlogsByTheme(String pageIndex,int id,HttpServletRequest request){
		request.setAttribute("map", foregroundService.getPageListByTheme(PageHelper.pageIndex(pageIndex),id));    //获取博客
	    request.setAttribute("timesTop", foregroundService.timesTop());  //浏览排行
        request.setAttribute("tags", foregroundService.tags());  //标签
        request.setAttribute("action","getBlogsByTheme.do" ); //设置分页动态action
		return "forward:page/blogByTheme.jsp";
	}
}
